package com.supermap.Attention.service.impl;

import com.supermap.Attention.service.AttentionService;
import com.supermap.attention.dao.AttentionDao;
import com.supermap.attention.entity.Attention;
import com.supermap.common.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lxs
 * @Title:
 * @Description:
 * @date 2018/2/27  14:34
 */
@Service
public class AttentionServiceImpl implements AttentionService {

    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(AttentionServiceImpl.class);
    @Resource
    private AttentionDao attentionDao;

    @Override
    public boolean saveAttention(Attention attention) {
//        boolean flag = true;
//        List<Attention> attentions = attentionDao.getAttentionsByUserId(attention.getUserid());//得到当前所有的关注物
//        List<String> lcropid=new ArrayList<>();//该用户已经关了的作物id 存入List
//        List<String> lareaid=new ArrayList<>();//该用户已经关了的区域id 存入List
//        for (int i = 0; i <attentions.size() ; i++) {
//            String cropid=attentions.get(i).getCropid();
//            String[] cropids=cropid.split(",");//该用户已经关了的作物id
//            String areaids=attentions.get(i).getAreaid();
//            for (int j = 0; j <cropids.length ; j++) {
//                lcropid.add(cropids[j]);
//            }
//            lareaid.add(areaids);
//        }
//            String[] czcropids=attention.getCropid().split(",");//当前传值过来的cropid
//            String czareaid=attention.getAreaid();
//                for (int k = 0; k <czcropids.length ; k++) {
//                    if(lcropid.contains(czcropids[k]) && lareaid.contains(czareaid)){//判断一个区域不能添加相同的作物
//                        flag=false;
//                    }
//                }
//                if(flag){
//                        int a = attentionDao.saveAttention(attention);
//                        if (a == 1) {
//                            return true;
//                        }
//                        return false;
//                }
        boolean flag = true;
        List<Attention> attentions = attentionDao.getAttentionsByUserId(attention.getUserid());//得到当前所有的关注物
        for (int i = 0; i < attentions.size(); i++) {
            String cropid=attentions.get(i).getCropid();//已经有的cropid
            String areaid=attentions.get(i).getAreaid();//已经有的areapid
            if(attention.getCropid().equals(cropid) && attention.getAreaid().equals(areaid)){
                flag=false;
            }
        }
        if(flag){
            int a = attentionDao.saveAttention(attention);
            if(a==1){
                return true;
            }
            return  false;
        }

        return false;
    }

    @Override
    public int getUserIdByuserName(String loginName) {
        return attentionDao.getUserIdByuserName(loginName);
    }

    @Override
    public JSONUtil getAttentionsByUserId(int userid) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<Attention> attentions = attentionDao.getAttentionsByUserId(userid);
                jsonUtil.setMsg("Get attentions success！");
                jsonUtil.setIsSuccess(true);
                jsonUtil.setCode(200);
                jsonUtil.setData(attentions);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("AttentionServiceImpl=>getAttentionList=>DaoException:" + e.getStackTrace());
            jsonUtil.setMsg("Get attentions flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public boolean deleteByCropId(int id) {
        int a = attentionDao.deleteByCropId(id);
        if (a == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updateAttentionById(Attention attention) {
        int a=attentionDao.updateAttentionById(attention);
        if(a==1){
            return true;
        }
        return false;
    }

}
